package com.example.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author admin
 */


@Aspect
@Component
public class TimeCustomAspect {
    @Before("execution(* com.example.demo.controller.*.*(..))") //包com.lingyi.mybatis.controller下的所有类和类中的方法都满足该AOP的条件
    public void beforeAdvice(){
        System.out.println("方法执行前.....");
    }
    @After("execution(* com.example.demo.controller.*.*(..))")
    public  void afterAdvice(){
        System.out.println("方法执行后.......");
    }
    @Around("execution(* com.example.demo.controller.*.*(..))")
    public Object recordTimeCustom(ProceedingJoinPoint pjp) throws  Throwable{
        long start = System.currentTimeMillis();
        Object result = pjp.proceed();
        long end = System.currentTimeMillis();

        String className = pjp.getTarget().getClass().getName();
        String method = pjp.getSignature().getName();
        String args = Arrays.toString(pjp.getArgs());
        System.out.println("类："+className + ",方法："+method +",参数："+args +",执行耗时："+ (end - start));
        return result;
    }

    @Before("@annotation(com.example.demo.anno.Loggin)")
    public void beforeAdvice2(){
        System.out.println("当前注解被使用了");
    }
}

